package com.qf.flash.ui.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.qf.flash.R;
import com.qf.flash.constant.Constants;
import com.qf.flash.utils.MyUtil;
import com.qf.flash.widget.ClearEditText;
import com.qf.flashlib.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditNicknameActivity extends BaseActivity {

    @BindView(R.id.tv_top_back)
    TextView tvTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.et_content)
    ClearEditText etContent;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_nikename;
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("编辑昵称");
        String nickName = getIntent().getStringExtra("nickName");
        etContent.setText(nickName);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.tv_top_back)
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.tv_top_back:
                String newName = etContent.getText().toString().trim();
                if (!TextUtils.isEmpty(newName)) {
                    Intent intent = new Intent();
                    intent.putExtra(Constants.CHANGED_NICKNAME, newName);
                    setResult(RESULT_OK, intent);
                }
                finish();
                break;
        }
    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            String newName = etContent.getText().toString().trim();
            if (!TextUtils.isEmpty(newName)) {
                Intent intent = new Intent();
                intent.putExtra(Constants.CHANGED_NICKNAME, newName);
                setResult(RESULT_OK, intent);
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
